package com.wwj.server.game.session;

import com.google.gson.GsonBuilder;
import com.google.protobuf.GeneratedMessage;
import com.wwj.server.model.Message;
import com.wwj.server.model.NetBaseMsg;
import com.wwj.server.serial.Serializer;
import com.wwj.server.session.Session;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 会话管理者
 *
 * @author -琴兽-
 */
public class SessionManager {

    public static final String PREFIX_SESSION_KEY_USER = "User@";

    /**
     * 在线会话
     */
    private static final ConcurrentHashMap<String, Session> onlineSessions = new ConcurrentHashMap<>();

    /**
     * 加入
     *
     * @param sessionKey
     * @param session
     * @return
     */
    public static boolean putSession(String sessionKey, Session session) {
        if (!onlineSessions.containsKey(sessionKey)) {
            boolean success = onlineSessions.putIfAbsent(sessionKey, session) == null ? true : false;
            return success;
        }
        return false;
    }

    public static boolean replaceSession(String sessionKey, Session newSession) {
        if (onlineSessions.containsKey(sessionKey)) {
            Session oldSession = onlineSessions.get(sessionKey);
            boolean success = onlineSessions.replace(sessionKey, oldSession, newSession);
            newSession.setAttachment(oldSession.getAttachment());
            oldSession.setAttachment(null);
            oldSession.close();
            return success;
        }
        return false;
    }

    /**
     * 移除
     *
     * @param sessionKey
     */
    public static Session removeSession(String sessionKey) {
        return onlineSessions.remove(sessionKey);
    }

    /**
     * 发送消息[自定义协议]
     *
     * @param <T>
     * @param sessionKey
     * @param message
     */
    public static <T extends Serializer> void sendMessage(String sessionKey, T message) {
        Session session = onlineSessions.get(sessionKey);
        if (session != null && session.isConnected()) {
            Message msg = new Message(message.getBytes());
            session.write(msg);
        }
    }

    /**
     * 发送消息[protoBuf协议]
     *
     * @param <T>
     * @param sessionKey
     * @param message
     */
    public static <T extends GeneratedMessage> void sendMessage(String sessionKey, T message) {
        Session session = onlineSessions.get(sessionKey);
        if (session != null && session.isConnected()) {
            Message msg = new Message(message.toByteArray());
            session.write(msg);
        }
    }

    public static <T extends NetBaseMsg> void sendMessage(String sessionKey, T message) {
        Session session = onlineSessions.get(sessionKey);
        if (session != null && session.isConnected()) {
            Message msg = new Message(new GsonBuilder()
                    .disableHtmlEscaping()
                    .serializeNulls().create().toJson(message).getBytes());
            session.write(msg);
        }
    }

    public static <T extends NetBaseMsg> void sendMessage(Session session, T message) {
        if (session != null && session.isConnected()) {
            Message msg = new Message(new GsonBuilder()
                    .disableHtmlEscaping()
                    .serializeNulls().create().toJson(message).getBytes());
            session.write(msg);
        }
    }

    /**
     * 是否在线
     *
     * @param sessionKey
     * @return
     */
    public static boolean isOnline(String sessionKey) {
        return onlineSessions.containsKey(sessionKey);
    }

    /**
     * 获取所有在线session
     *
     * @return
     */
    public static Set<String> getOnline() {
        return Collections.unmodifiableSet(onlineSessions.keySet());
    }

    public static Session getSession(String sessionKey) {
        return onlineSessions.containsKey(sessionKey) ? onlineSessions.get(sessionKey) : null;
    }
}
